package com.example.supernote.ui.settings

import android.content.Context
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import com.example.supernote.R
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.DarkMode
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Public
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.supernote.model.settings.LanguageType
import com.example.supernote.model.settings.ModeType
import com.example.supernote.ui.components.UniformButton
import com.example.supernote.ui.components.UniformIcon


data class SettingItem(
    val type: Int,
    val icon: ImageVector,
    val title: String,
    val subtitle: String
)

@Composable
fun SuperNoteSettings(
    modifier: Modifier = Modifier,
    uiState: State<SettingsUiState>,
    changeModel: (selectModel: ModeType) -> Unit,
    changeLanguage: (selectLanguage: LanguageType, context: Context) -> Unit
) {
    val settingsList = listOf(
        SettingItem(1, Icons.Filled.DarkMode, stringResource(id = R.string.settings_model), stringResource(id = R.string.settings_submodel)),
        SettingItem(2, Icons.Filled.Public, stringResource(id = R.string.settings_language), stringResource(id = R.string.settings_sublanguage)),
        SettingItem(3, Icons.Filled.Info, stringResource(id = R.string.settings_about), stringResource(id = R.string.settings_subabout))
    )
    var select by remember {
        mutableIntStateOf(0)
    }
    val selectModel = uiState.value.selectModel
    val selectLanguage = uiState.value.selectLanguage
    Column(modifier = modifier) {
        HorizontalDivider(modifier = Modifier)
        val itemModifier = Modifier
            .padding(start = 5.dp, end = 5.dp)
            .heightIn(min = 48.dp)
        if (select == 0) {
            LazyColumn {
                items(settingsList) { settingsitem ->
                    SuperNoteSettingsMain(
                        modifier = itemModifier,
                        settingsItem = settingsitem,
                        onSelectChange = { select = it })
                }
            }
        } else if (select == 1) {
            SuperNoteModel(
                modifier = itemModifier,
                onSelectChange = { select = it },
                selectModel = selectModel,
                changeModel = changeModel
            )
        } else if (select == 2) {
            SuperNoteLanguage(
                modifier = itemModifier,
                onSelectChange = { select = it },
                selectLanguage = selectLanguage,
                changeLanguage = changeLanguage
            )
        } else if (select == 3) {
            SuperNoteInfo(modifier = itemModifier, onSelectChange = { select = it })
        }
    }

}


@Composable
fun SuperNoteSettingsMain(
    modifier: Modifier = Modifier,
    settingsItem: SettingItem,
    onSelectChange: (Int) -> Unit
) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .height(70.dp)
            .clickable { onSelectChange(settingsItem.type) },
        verticalAlignment = Alignment.CenterVertically
    ) {
        UniformIcon(
            modifier = modifier
                .padding(end = 10.dp)
                .size(40.dp), imageVector = settingsItem.icon
        )
        Text(text = buildAnnotatedString {
            withStyle(
                style = SpanStyle(
                    fontSize = 18.sp,
                    color = MaterialTheme.colorScheme.onBackground
                )
            ) {
                append(settingsItem.title)
            }
            append("\n")
            withStyle(
                style = SpanStyle(
                    fontSize = 14.sp,
                    color = MaterialTheme.colorScheme.onBackground
                )
            ) {
                append(settingsItem.subtitle)
            }
        }, lineHeight = 16.sp)
    }
}


@Composable
fun SuperNoteModel(
    modifier: Modifier = Modifier, onSelectChange: (Int) -> Unit,
    selectModel: ModeType,
    changeModel: (selectModel: ModeType) -> Unit
) {
    val modelList = ModeData
    var selectedModel by remember { mutableStateOf<ModeType>(selectModel) }
    Column(modifier = modifier.fillMaxWidth()) {
        UniformButton(onClick = { onSelectChange(0) }, imageVector = Icons.Filled.ArrowBack)
        HorizontalDivider(modifier = Modifier)
        LazyColumn {
            items(modelList) { model ->
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(48.dp)
                        .clickable {
                            selectedModel = model
                            changeModel(model)
                        },
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = when (model) {
                            ModeType.Light -> stringResource(id = R.string.settings_light)
                            ModeType.Dark -> stringResource(id = R.string.settings_dark)
                            ModeType.FollowSystem -> stringResource(id = R.string.settings_followSystem)
                        },
                        modifier = Modifier.padding(start = 10.dp),
                        color = MaterialTheme.colorScheme.onBackground
                    )
                    if (selectedModel == model) {
                        UniformIcon(
                            modifier = Modifier.padding(end = 10.dp),
                            imageVector = Icons.Filled.Check
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun SuperNoteLanguage(
    modifier: Modifier = Modifier, onSelectChange: (Int) -> Unit,
    selectLanguage: LanguageType,
    changeLanguage: (selectLanguage: LanguageType, context:Context) -> Unit
) {
    val languageList = LanguageData
    var selectedLanguage by remember { mutableStateOf<LanguageType>(selectLanguage) }
    val context = LocalContext.current
    Column(modifier = modifier.fillMaxWidth()) {
        UniformButton(onClick = { onSelectChange(0) }, imageVector = Icons.Filled.ArrowBack)
        HorizontalDivider(modifier = Modifier)
        LazyColumn {
            items(languageList) { language ->
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(48.dp)
                        .clickable {
                            selectedLanguage = language
                            changeLanguage(language,context)
                        },
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = when (language) {
                            LanguageType.Chinese -> stringResource(id = R.string.settings_Chinese)
                            LanguageType.English -> stringResource(id = R.string.settings_English)
                            LanguageType.FollowSystem -> stringResource(id = R.string.settings_followSystem)
                        },
                        modifier = Modifier.padding(start = 10.dp),
                        color = MaterialTheme.colorScheme.onBackground
                    )
                    if (selectedLanguage == language) {
                        UniformIcon(
                            modifier = Modifier.padding(end = 10.dp),
                            imageVector = Icons.Filled.Check
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun SuperNoteInfo(
    modifier: Modifier = Modifier, onSelectChange: (Int) -> Unit
) {
    Column(modifier = modifier.fillMaxWidth()) {
        UniformButton(onClick = { onSelectChange(0) }, imageVector = Icons.Filled.ArrowBack)
        HorizontalDivider(modifier = Modifier)
        val textModifier = Modifier.padding(horizontal = 5.dp, vertical = 10.dp)
        Text(
            text = stringResource(id = R.string.settings_version),
            modifier = textModifier,
            color = MaterialTheme.colorScheme.onBackground
        )
        Text(
            text = stringResource(id = R.string.settings_team),
            modifier = textModifier,
            color = MaterialTheme.colorScheme.onBackground
        )
        Text(
            text = stringResource(id = R.string.settings_privacy),
            modifier = textModifier,
            color = MaterialTheme.colorScheme.onBackground
        )
    }
}


